package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.edu.entity.Video;
import com.atguigu.guli.service.edu.feign.VodMediaService;
import com.atguigu.guli.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author wangyang
 * @since 2021-05-27
 */
@CrossOrigin
@Api(tags = "课时管理")
@Slf4j
@RestController
@RequestMapping("admin/edu/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @ApiOperation("保存课时")
    @PostMapping("/save")
    public R saveVideo(
            @ApiParam(value = "课时",required = true)
            @RequestBody Video video
            ) {
            boolean result = videoService.save(video);
        if (result) {
            return R.ok().message("添加成功！");
        }else {
            return  R.error().message("添加失败！");
        }
    }

    @ApiOperation("根据Id查询课时")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "课时id",required = true)
            @PathVariable String id
    ) {
        Video result = videoService.getById(id);
        if (result != null) {
            return R.ok().data("item", result);
        } else {
            return R.error().message("数据不存在");
        }
    }
    @ApiOperation("根据Id更新课时")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "课时",required = true)
            @RequestBody Video video
    ) {
        boolean result = videoService.updateById(video);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据ID删除课时")
    @DeleteMapping("remove/{id}")
    public R removeById(
        @ApiParam(value = "课时ID", required = true)
        @PathVariable String id){
        // 删除视频：VOD
        //在此处调用vod中的删除视频文件的接口
        videoService.removeMediaVideoById(id);
        boolean result = videoService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }


}

